package Format;

import bean.HotMovie;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexFormat {
    private String match_str;
    private String regex;
    private String regex_director;
    private String regex_introduction;

    public RegexFormat() {

    }

    public RegexFormat(String match_str, String regex) {
        this.match_str = match_str;
        this.regex = regex;
    }

    public RegexFormat(String match_str) {
        this.match_str = match_str;
        this.regex_director = "rel=\"v:directedBy\">(.*?)</a>";
        this.regex_introduction = "<span property=\"v:summary\" class=\"\">(.*?)</span>";
    }

    public HotMovie findDirector(HotMovie movie) {
        Pattern pattern = Pattern.compile(regex_director);
        Matcher matcher = pattern.matcher(match_str);
        while (matcher.find()) {
            movie.setDirector(matcher.group(1).trim());
        }

        return movie;
    }

    public HotMovie findIntroduction(HotMovie movie) {
        Pattern pattern = Pattern.compile(regex_introduction);
        Matcher matcher = pattern.matcher(match_str);
        while (matcher.find()) {
            movie.setIntroduction(matcher.group(1).trim());
        }
        return movie;
    }

    public void find() {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(match_str);
        int i = 0;
        while (matcher.find()) {
            i++;
            System.out.println("匹配的第" + i + "个:" + matcher.group());
        }
    }
}
